package com.demon.androidbasic.toast;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;

import com.demon.logger.Logger;

/**
 * Created by justin on 17/9/11.
 * Toast工具类
 */
public class ToastUtils {

    private static final String TAG = "ToastUtils";
    private static Toast mToast;

    private ToastUtils() {
        throw new AssertionError();
    }

    public static Toast showToast(Context context, int stringResId) {
        return showToast(context, stringResId, Duration.SHORT);
    }

    public static Toast showToast(Context context, int stringResId, Duration duration) {
        if (context == null) {
            return null;
        }
        return showToast(context, context.getString(stringResId), duration);
    }

    public static Toast showToast(Context context, String text) {
        return showToast(context, text, Duration.SHORT);
    }

    public static Toast showToast(Context context, String text, Duration duration) {
        if (context == null || TextUtils.isEmpty(text)) {
            return null;
        }
        Logger.INSTANCE.d(TAG, "mToast  " + mToast);
        if (mToast == null) {
            mToast = Toast.makeText(context.getApplicationContext(), text, (duration == Duration.SHORT ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG)
            );
        }
        try {
            mToast.setText(text);
            mToast.setDuration(duration == Duration.SHORT ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG);
            mToast.setGravity(Gravity.CENTER, 0, 0);
            mToast.show();
            Logger.INSTANCE.d(TAG, "show mToast  " + mToast);
        } catch (Exception e) {
            e.printStackTrace();
            Logger.INSTANCE.e(TAG, "show toast error", e);
        }
        return mToast;
    }

    //public static Toast showToast(String text) {
    //    return showToast(BaseApplication.getContext(), text, Duration.SHORT);
    //}
    //
    //public static Toast showToast(int id) {
    //    Context context = BaseApplication.getContext();
    //    return showToast(context, context.getString(id), Duration.SHORT);
    //}

    public enum Duration {
        SHORT,
        LONG
    }
}
